package com.mxx.common.utils;

import com.mxx.common.conv.JacksonConverter;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Auther: Mr. Zeng
 * @Date: 2020/10/29 14:27
 * @Description: 暂无描述
 */
public class Maps<K, V> {
    private Map<K, V> map;
    private JacksonConverter converter = JacksonConverter.getInstance();

    private Maps(Map<K, V> map) {
        this.map = map;
    }

    public static <K, V> Maps<K, V> create(Map<K, V> map) {
        return new Maps<>(map);
    }

    public static <K, V> Maps<K, V> hash() {
        return new Maps<>(new HashMap<>());
    }

    public static <K, V> Maps<K, V> hash(int size) {
        int initialCapacity = (int) ((size / 0.75f) + 1);
        return new Maps<>(new HashMap<>(initialCapacity));
    }

    public static <K, V> Maps<K, V> tree() {
        return new Maps<>(new TreeMap<>());
    }

    public Maps<K, V> put(K key, V val) {
        this.map.put(key, val);
        return this;
    }

    public Maps<K, V> putIf(boolean bool, K key, V val) {
        if (bool) {
            this.map.put(key, val);
        }
        return this;
    }

    public Maps<K, V> put(Map<K, V> map) {
        this.map.putAll(map);
        return this;
    }

    /**
     * 获取当前值
     *
     * @param key
     * @return
     */

    public V get(K key) {
        return this.map.get(key);
    }

    /**
     * 获取当前值的String 值
     *
     * @param key
     * @return
     */
    public String getString(K key) {
        return this.converter.convertValue(this.get(key), String.class);
    }

    /**
     * 获取当前值的Boolean 值
     *
     * @param key
     * @return
     */
    public Boolean getBoolean(K key) {
        return this.converter.convertValue(this.get(key), Boolean.class);
    }

    /**
     * 获取当前值的Long 值
     *
     * @param key
     * @return
     */
    public Long getLong(K key) {
        return this.converter.convertValue(this.get(key), Long.class);
    }

    /**
     * 获取当前值的Integer 值
     *
     * @param key
     * @return
     */
    public Integer getInteger(K key) {
        return this.converter.convertValue(this.get(key), Integer.class);
    }

    /**
     * 获取当前值的Double 值
     *
     * @param key
     * @return
     */
    public Double getDouble(K key) {
        return this.converter.convertValue(this.get(key), Double.class);
    }

    /**
     * 获取当前值的BigDecimal 值
     *
     * @param key
     * @return
     */
    public BigDecimal getBigDecimal(K key) {
        return this.converter.convertValue(this.get(key), BigDecimal.class);
    }

    /**
     * 获取当前值的LocalDate 值
     *
     * @param key
     * @return
     */
    public LocalDate getLocalDate(K key) {
        return this.converter.convertValue(this.get(key), LocalDate.class);
    }

    /**
     * 获取当前值的 LocalDateTime 值
     *
     * @param key
     * @return
     */
    public LocalDateTime getLocalDateTime(K key) {
        return this.converter.convertValue(this.get(key), LocalDateTime.class);
    }


    public Maps<K, V> clear() {
        this.map.clear();
        return this;
    }

    public Map<K, V> val() {
        return this.map;
    }

    public String ascii() {
        return ascii(map);
    }

    public static String ascii(Map map) {
        Map<String, Object> sortMap = new TreeMap<>(map);
        Iterator<String> iterator = sortMap.keySet().iterator();
        StringBuffer sb = new StringBuffer();
        while (iterator.hasNext()) {
            String key = iterator.next();
            Object val = map.get(key);
            if (val == null) {
                continue;
            }
            sb.append(key).append("=").append(val)
                    .append("&");
        }
        if (sortMap.isEmpty()) {
            return sb.toString();
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

}
